package JavaDesignModel;
//  实验四   建造者模式


// 1. 产品类 - Computer（电脑主机）
class Computer {
    private String cpu;         // CPU
    private String memory;      // 内存
    private String motherboard; // 主板
    private String storage;     // 硬盘
    private String graphics;   // 独立显卡
    private String powerSupply; // 电源
    private String cooling;    // 散热

    public void setCpu(String cpu) { this.cpu = cpu; }
    public void setMemory(String memory) { this.memory = memory; }
    public void setMotherboard(String motherboard) { this.motherboard = motherboard; }
    public void setStorage(String storage) { this.storage = storage; }
    public void setGraphics(String graphics) { this.graphics = graphics; }
    public void setPowerSupply(String powerSupply) { this.powerSupply = powerSupply; }
    public void setCooling(String cooling) { this.cooling = cooling; }

    public String getCpu() { return cpu; }
    public String getMemory() { return memory; }
    public String getMotherboard() { return motherboard; }
    public String getStorage() { return storage; }
    public String getGraphics() { return graphics; }
    public String getPowerSupply() { return powerSupply; }
    public String getCooling() { return cooling; }

    public void showConfig() {
        System.out.println("当前电脑主机配置:");
        System.out.println("CPU: " + cpu);
        System.out.println("内存: " + memory);
        System.out.println("主板: " + motherboard);
        System.out.println("硬盘: " + storage);
        System.out.println("独立显卡: " + graphics);
        System.out.println("电源: " + powerSupply);
        System.out.println("散热: " + cooling);
        System.out.println();
    }
}

// 2. 抽象建造者 - ComputerBuilder
abstract class ComputerBuilder {
    protected Computer computer = new Computer();

    public abstract void buildCpu();
    public abstract void buildMemory();
    public abstract void buildMotherboard();
    public abstract void buildStorage();
    public abstract void buildGraphics();
    public abstract void buildPowerSupply();
    public abstract void buildCooling();

    // 工厂方法，返回完整的电脑对象
    public Computer createComputer() {
        return computer;
    }
}

// 3. 具体建造者 - GameComputerBuilder（游戏主机建造者）
class GameComputerBuilder extends ComputerBuilder {
    public void buildCpu() {
        computer.setCpu("Intel i9-13900K");
    }
    public void buildMemory() {
        computer.setMemory("32GB DDR5");
    }
    public void buildMotherboard() {
        computer.setMotherboard("微星主板");
    }
    public void buildStorage() {
        computer.setStorage("1TB NVMe SSD");
    }
    public void buildGraphics() {
        computer.setGraphics("NVIDIA RTX 4090");
    }
    public void buildPowerSupply() {
        computer.setPowerSupply("1000w金牌电源");
    }
    public void buildCooling() {
        computer.setCooling("水冷");
    }
}

// 4. 具体建造者 - OfficeComputerBuilder（办公主机建造者）
class OfficeComputerBuilder extends ComputerBuilder {
    public void buildCpu() {
        computer.setCpu("Intel i5-12400");
    }
    public void buildMemory() {
        computer.setMemory("16GB DDR4");
    }
    public void buildMotherboard() {
        computer.setMotherboard("华硕主板");
    }
    public void buildStorage() {
        computer.setStorage("512GB SSD");
    }
    public void buildGraphics() {
        computer.setGraphics("无");
    }
    public void buildPowerSupply() {
        computer.setPowerSupply("500w铜牌电源");
    }
    public void buildCooling() {
        computer.setCooling("风扇");
    }
}

// 5. 具体建造者 - WebComputerBuilder（上网主机建造者）
class WebComputerBuilder extends ComputerBuilder {
    public void buildCpu() {
        computer.setCpu("Intel i3-12100");
    }
    public void buildMemory() {
        computer.setMemory("8GB DDR4");
    }
    public void buildMotherboard() {
        computer.setMotherboard("华擎主板");
    }
    public void buildStorage() {
        computer.setStorage("500GB SATA");
    }
    public void buildGraphics() {
        computer.setGraphics("无");
    }
    public void buildPowerSupply() {
        computer.setPowerSupply("350w白牌电源");
    }
    public void buildCooling() {
        computer.setCooling("风扇");
    }
}

// 6. 指挥者 - ComputerDirector
class ComputerDirector {
    public Computer construct(ComputerBuilder builder) {
        builder.buildCpu();
        builder.buildMemory();
        builder.buildMotherboard();
        builder.buildStorage();
        builder.buildGraphics();
        builder.buildPowerSupply();
        builder.buildCooling();
        return builder.createComputer();
    }
}

// 7. 客户端使用
public class ComputerStore {
    public static void main(String[] args) {
        ComputerDirector director = new ComputerDirector();

        Computer gameComputer = director.construct(new GameComputerBuilder());
        System.out.println("游戏主机");
        gameComputer.showConfig();

        Computer officeComputer = director.construct(new OfficeComputerBuilder());
        System.out.println("办公主机");
        officeComputer.showConfig();

        Computer webComputer = director.construct(new WebComputerBuilder());
        System.out.println("上网主机");
        webComputer.showConfig();
    }
}
/*
支持链式调用自定义电脑主机


// 1. 产品类 - Computer（电脑主机）
class Computer {
    private String cpu;
    private String memory;
    private String motherboard;
    private String storage;
    private String graphics;
    private String powerSupply;
    private String cooling;

    public void setCpu(String cpu) { this.cpu = cpu; }
    public void setMemory(String memory) { this.memory = memory; }
    public void setMotherboard(String motherboard) { this.motherboard = motherboard; }
    public void setStorage(String storage) { this.storage = storage; }
    public void setGraphics(String graphics) { this.graphics = graphics; }
    public void setPowerSupply(String powerSupply) { this.powerSupply = powerSupply; }
    public void setCooling(String cooling) { this.cooling = cooling; }

    public String getCpu() { return cpu; }
    public String getMemory() { return memory; }
    public String getMotherboard() { return motherboard; }
    public String getStorage() { return storage; }
    public String getGraphics() { return graphics; }
    public String getPowerSupply() { return powerSupply; }
    public String getCooling() { return cooling; }

    public void showConfig() {
        System.out.println("当前电脑主机配置:");
        System.out.println("CPU: " + cpu);
        System.out.println("内存: " + memory);
        System.out.println("主板: " + motherboard);
        System.out.println("硬盘: " + storage);
        System.out.println("独立显卡: " + graphics);
        System.out.println("电源: " + powerSupply);
        System.out.println("散热: " + cooling);
        System.out.println();
    }
}

// 2. 抽象建造者 - ComputerBuilder（支持链式调用）
abstract class ComputerBuilder {
    protected Computer computer = new Computer();

    public abstract ComputerBuilder buildCpu();
    public abstract ComputerBuilder buildMemory();
    public abstract ComputerBuilder buildMotherboard();
    public abstract ComputerBuilder buildStorage();
    public abstract ComputerBuilder buildGraphics();
    public abstract ComputerBuilder buildPowerSupply();
    public abstract ComputerBuilder buildCooling();

    // 支持自定义部件的链式调用方法
    public ComputerBuilder customCpu(String cpu) {
        computer.setCpu(cpu);
        return this;
    }

    public ComputerBuilder customMemory(String memory) {
        computer.setMemory(memory);
        return this;
    }

    public ComputerBuilder customMotherboard(String motherboard) {
        computer.setMotherboard(motherboard);
        return this;
    }

    public ComputerBuilder customStorage(String storage) {
        computer.setStorage(storage);
        return this;
    }

    public ComputerBuilder customGraphics(String graphics) {
        computer.setGraphics(graphics);
        return this;
    }

    public ComputerBuilder customPowerSupply(String powerSupply) {
        computer.setPowerSupply(powerSupply);
        return this;
    }

    public ComputerBuilder customCooling(String cooling) {
        computer.setCooling(cooling);
        return this;
    }

    // 工厂方法，返回完整的电脑对象
    public Computer createComputer() {
        return computer;
    }
}

// 3. 具体建造者 - GameComputerBuilder（游戏主机建造者）
class GameComputerBuilder extends ComputerBuilder {
    public ComputerBuilder buildCpu() {
        computer.setCpu("Intel i9-13900K");
        return this;
    }
    public ComputerBuilder buildMemory() {
        computer.setMemory("32GB DDR5");
        return this;
    }
    public ComputerBuilder buildMotherboard() {
        computer.setMotherboard("微星主板");
        return this;
    }
    public ComputerBuilder buildStorage() {
        computer.setStorage("1TB NVMe SSD");
        return this;
    }
    public ComputerBuilder buildGraphics() {
        computer.setGraphics("NVIDIA RTX 4090");
        return this;
    }
    public ComputerBuilder buildPowerSupply() {
        computer.setPowerSupply("1000w金牌电源");
        return this;
    }
    public ComputerBuilder buildCooling() {
        computer.setCooling("水冷");
        return this;
    }
}

// 4. 具体建造者 - OfficeComputerBuilder（办公主机建造者）
class OfficeComputerBuilder extends ComputerBuilder {
    public ComputerBuilder buildCpu() {
        computer.setCpu("Intel i5-12400");
        return this;
    }
    public ComputerBuilder buildMemory() {
        computer.setMemory("16GB DDR4");
        return this;
    }
    public ComputerBuilder buildMotherboard() {
        computer.setMotherboard("华硕主板");
        return this;
    }
    public ComputerBuilder buildStorage() {
        computer.setStorage("512GB SSD");
        return this;
    }
    public ComputerBuilder buildGraphics() {
        computer.setGraphics("无");
        return this;
    }
    public ComputerBuilder buildPowerSupply() {
        computer.setPowerSupply("500w铜牌电源");
        return this;
    }
    public ComputerBuilder buildCooling() {
        computer.setCooling("风扇");
        return this;
    }
}

// 5. 具体建造者 - WebComputerBuilder（上网主机建造者）
class WebComputerBuilder extends ComputerBuilder {
    public ComputerBuilder buildCpu() {
        computer.setCpu("Intel i3-12100");
        return this;
    }
    public ComputerBuilder buildMemory() {
        computer.setMemory("8GB DDR4");
        return this;
    }
    public ComputerBuilder buildMotherboard() {
        computer.setMotherboard("华擎主板");
        return this;
    }
    public ComputerBuilder buildStorage() {
        computer.setStorage("500GB SATA");
        return this;
    }
    public ComputerBuilder buildGraphics() {
        computer.setGraphics("无");
        return this;
    }
    public ComputerBuilder buildPowerSupply() {
        computer.setPowerSupply("350w白牌电源");
        return this;
    }
    public ComputerBuilder buildCooling() {
        computer.setCooling("风扇");
        return this;
    }
}

// 6. 指挥者 - ComputerDirector
class ComputerDirector {
    public Computer construct(ComputerBuilder builder) {
        builder.buildCpu()
               .buildMemory()
               .buildMotherboard()
               .buildStorage()
               .buildGraphics()
               .buildPowerSupply()
               .buildCooling();
        return builder.createComputer();
    }
}

// 7. 客户端使用
public class ComputerStore {
    public static void main(String[] args) {
        ComputerDirector director = new ComputerDirector();

        Computer gameComputer = director.construct(new GameComputerBuilder());
        System.out.println("游戏主机");
        gameComputer.showConfig();

        Computer officeComputer = director.construct(new OfficeComputerBuilder());
        System.out.println("办公主机");
        officeComputer.showConfig();

        Computer webComputer = director.construct(new WebComputerBuilder());
        System.out.println("上网主机");
        webComputer.showConfig();

        System.out.println("=== DIY自定义电脑 ===");

        // 基于预设配置的DIY（修改部分配置）
         Computer diyComputer1 = new GameComputerBuilder()
            .buildCpu()  // 使用预设的游戏CPU
            .buildMemory()  // 使用预设的游戏内存
            .customStorage("2TB NVMe SSD")  // 自定义硬盘
            .customGraphics("AMD RX 7900 XTX")  // 自定义显卡
            .buildPowerSupply()  // 使用预设的电源
            .buildCooling()  // 使用预设的散热
            .createComputer();
        System.out.println("DIY游戏电脑（修改版）");
        diyComputer1.showConfig();

        // 完全自定义配置
        Computer diyComputer2 = new GameComputerBuilder()
            .customCpu("AMD Ryzen 9 7950X")
            .customMemory("64GB DDR5")
            .customMotherboard("技嘉主板")
            .customStorage("4TB NVMe SSD")
            .customGraphics("NVIDIA RTX 4090")
            .customPowerSupply("1200w铂金电源")
            .customCooling("分体式水冷")
            .createComputer();
        System.out.println("自定义电脑");
        diyComputer2.showConfig();
    }
}
 */